'''
    自动取款机存取模拟系统
    jam_pang
'''
password_flag = 0
cus_index = 0


# 1.客户信息数据源
customer_list = [
    {'name':'zhangsan', 'balance':10,'password':'root'},
    {'name':'lisi', 'balance':20,'password':'root'},
    {'name':'wangwu', 'balance':30,'password':'root'}]

# 2.查询余额
def show_customer_info():
    # if len(customer_list) == 0:
    #     print('='*20,'无客户信息','='*20)
    # else:
    #     print('|{:<5}|{:<10}|{:<10}|'.format('id','name','balance'))
    #     print('-'*40)
    #     for i,cus_dict in enumerate(customer_list):
    #         print('|{:<5}|{:<10}|{:<10}|'.format(i+1,cus_dict['name'],cus_dict['balance']))
    print('|{:<10}|{:<10}|'.format('name', 'balance'))
    print('-' * 40)
    print('|{:<10}|{:<10}|'.format(customer_list[cus_index].get('name'), customer_list[cus_index].get('balance')))

# 3.存款功能
def add_customer(balance):
    # input的数值是String类型，需要转成int
    customer_list[cus_index]['balance'] += int(balance)

# 4.取款功能
def del_cus(balance):
    customer_list[cus_index]['balance'] -= int(balance)

# 5.退出系统
def loginOut():
    print('=' * 20, '请取好你的卡片', '=' * 20)
    exit()

# 6.界面系统
while True:
    if password_flag == 0:
        account = input('请输入客户账号:')
        # pycharm编译器不支持使用getpass，只好用明文显示
        password = input('请输入客户密码:')
        for i in enumerate(customer_list):
            i=list(i)
            # 第0位返回的是下标，第1位返回的是对应的列表
            if((i[1].get('password') == password)and(i[1].get('name') == account)):
                password_flag = 1
                cus_index = i[0]

    if(password_flag == 0):
        print('*' * 12, '错误提示', '*' * 12)
        print('账户或密码错误，请重新再试')
        continue

    print('='*12,'自动取款机存取模拟系统','='*12)
    print('{:1}{:13}{:15}'.format(' ', '1. 查看余额信息', '2. 存款'))
    print('{:1}{:13}{:15}'.format(' ', '3. 取款', '4. 退出系统'))
    print('='*47)
    global key
    key = input('请输入对应的选择:')

    #根据输入值进行相应操作
    if key == '1':
        print('=' * 12, '客户信息预览', '=' * 12)
        show_customer_info()
        print('=' * 47)
        input('按回车继续: ')
    elif key == '2':
        print('=' * 12, '客户信息预览', '=' * 12)
        balance = input('请输入存款金额:')
        if('.' in balance):
            print('*' * 12, '错误提示', '*' * 12)
            print('操作无效，输入值只能为整数！')
            continue
        add_customer(balance)
        print('=' * 47)
        input('按回车继续: ')
    elif key == '3':
        print('=' * 12, '客户信息预览', '=' * 12)
        balance = input('请输入取款金额：')
        if('.' in balance):
            print('*' * 12, '错误提示', '*' * 12)
            print('操作无效，输入值只能为整数！')
            continue
        if (customer_list[cus_index].get('balance') - int(balance) < 0):
            print('*' * 12, '错误提示', '*' * 12)
            print('操作无效，余额不足！')
            continue
        del_cus(balance)
        print('=' * 47)
        input('按回车继续: ')
    elif key == '4':
        loginOut()
        break
    else:
        print('*' * 12, '错误提示', '*' * 12)
        print('操作无效，请重新输入:')

